# -*- coding: utf-8 -*-
"""
自定义异常类
"""

from typing import Any, Dict, Optional
from fastapi import HTTPException, status


class BaseException(Exception):
    """基础异常类"""
    def __init__(self, message: str = "服务器内部错误", code: int = 500, data: Optional[Dict[str, Any]] = None):
        self.message = message
        self.code = code
        self.data = data or {}
        super().__init__(self.message)


class BusinessException(BaseException):
    """业务异常"""
    def __init__(self, message: str = "业务处理异常", code: int = 400, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class AuthenticationException(BaseException):
    """认证异常"""
    def __init__(self, message: str = "认证失败", code: int = 401, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class AuthorizationException(BaseException):
    """授权异常"""
    def __init__(self, message: str = "权限不足", code: int = 403, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class NotFoundException(BaseException):
    """资源不存在异常"""
    def __init__(self, message: str = "资源不存在", code: int = 404, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class ValidationException(BaseException):
    """数据验证异常"""
    def __init__(self, message: str = "数据验证失败", code: int = 422, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class DatabaseException(BaseException):
    """数据库异常"""
    def __init__(self, message: str = "数据库操作失败", code: int = 500, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


class TenantException(BaseException):
    """租户异常"""
    def __init__(self, message: str = "租户操作异常", code: int = 400, data: Optional[Dict[str, Any]] = None):
        super().__init__(message, code, data)


# FastAPI HTTP 异常转换器
def create_http_exception(exc: BaseException) -> HTTPException:
    """将自定义异常转换为 FastAPI HTTPException"""
    return HTTPException(
        status_code=exc.code,
        detail={
            "message": exc.message,
            "code": exc.code,
            "data": exc.data
        }
    )